package api

import (
	"github.com/gin-gonic/gin"
	"github.com/jiedan/student-web/server/middleware"
	"gorm.io/gorm"
)

// RegisterRoutes 注册所有API路由
func RegisterRoutes(r *gin.Engine, db *gorm.DB) {
	// 创建API处理器
	authHandler := NewAuthHandler(db)
	userHandler := NewUserHandler(db)
	roleHandler := NewRoleHandler(db)
	studentHandler := NewStudentHandler(db)
	courseHandler := NewCourseHandler(db)
	gradeHandler := NewGradeHandler(db)
	courseSelectionHandler := NewCourseSelectionHandler(db)

	// API分组
	api := r.Group("/api")

	// 公开路由
	auth := api.Group("/auth")
	{
		auth.POST("/login", authHandler.Login)
		auth.POST("/register", authHandler.Register)
	}

	// 需要认证的路由
	authRequired := api.Group("/")
	authRequired.Use(middleware.AuthMiddleware())
	{
		// 当前用户信息
		authRequired.GET("/user", authHandler.GetCurrentUser)

		// 用户管理 - 仅管理员
		users := authRequired.Group("/users")
		users.Use(middleware.AdminRequired())
		{
			users.GET("/", userHandler.GetUsers)
			users.GET("/:id", userHandler.GetUser)
			users.POST("/", userHandler.CreateUser)
			users.PUT("/:id", userHandler.UpdateUser)
			users.DELETE("/:id", userHandler.DeleteUser)
		}

		// 角色管理 - 仅管理员
		roles := authRequired.Group("/roles")
		roles.Use(middleware.AdminRequired())
		{
			roles.GET("/", roleHandler.GetRoles)
			roles.GET("/:id", roleHandler.GetRole)
			roles.POST("/", roleHandler.CreateRole)
			roles.PUT("/:id", roleHandler.UpdateRole)
			roles.DELETE("/:id", roleHandler.DeleteRole)
		}

		// 学生管理 - 管理员和教师
		students := authRequired.Group("/students")
		students.Use(middleware.TeacherRequired())
		{
			students.GET("/", studentHandler.GetStudents)
			students.GET("/:id", studentHandler.GetStudent)
			students.POST("/", studentHandler.CreateStudent)
			students.PUT("/:id", studentHandler.UpdateStudent)
			students.DELETE("/:id", studentHandler.DeleteStudent)
		}

		// 课程管理
		courses := authRequired.Group("/courses")
		{
			// 所有用户可以查看课程
			courses.GET("/", courseHandler.GetCourses)
			courses.GET("/:id", courseHandler.GetCourse)

			// 只有管理员和教师可以管理课程
			courseAdmin := courses.Group("/")
			courseAdmin.Use(middleware.TeacherRequired())
			{
				courseAdmin.POST("/", courseHandler.CreateCourse)
				courseAdmin.PUT("/:id", courseHandler.UpdateCourse)
				courseAdmin.DELETE("/:id", courseHandler.DeleteCourse)
			}
		}

		// 成绩管理
		grades := authRequired.Group("/grades")
		{
			// 学生只能查看自己的成绩
			grades.GET("/student", gradeHandler.GetStudentGrades)

			// 教师可以管理成绩
			gradeAdmin := grades.Group("/")
			gradeAdmin.Use(middleware.TeacherRequired())
			{
				//gradeAdmin.GET("/", gradeHandler.GetGrades)
				//gradeAdmin.GET("/:id", gradeHandler.GetGrade)
				gradeAdmin.POST("/", gradeHandler.CreateGrade)
				gradeAdmin.PUT("/:id", gradeHandler.UpdateGrade)
				gradeAdmin.DELETE("/:id", gradeHandler.DeleteGrade)
			}
		}

		// 选课管理
		selections := authRequired.Group("/selections")
		{
			// 学生选课操作
			studentSelections := selections.Group("/student")
			studentSelections.Use(middleware.StudentRequired())
			{
				studentSelections.GET("/", courseSelectionHandler.GetStudentSelections)
				studentSelections.POST("/", courseSelectionHandler.SelectCourse)
				studentSelections.DELETE("/:id", courseSelectionHandler.DropCourse)
			}

			// 教师查看选课情况
			teacherSelections := selections.Group("/")
			teacherSelections.Use(middleware.TeacherRequired())
			{
				teacherSelections.GET("/", courseSelectionHandler.GetAllSelections)
				teacherSelections.GET("/course/:id", courseSelectionHandler.GetCourseSelections)
			}
		}
	}
}
